#include <iostream>
#include <string>
#include <map>
using namespace std;

const int map_worker_count = 1; /* loop over all input files */
const int map_split_count = 2; /* 2 input files */
const int reduce_worker_count = 1; /* loop over all 26 maps whose key is the leading letter of a word */
const int reduce_split_count = 26; /* each file results in a map with [a-z] as keys */

int map_current_split = 0;
map<char, int> count_map[map_split_count];
map<char, int> result_map;

/*
 * K1 = file name
 * V1 = file content
 * -->
 * K2 = leading letter of a word
 * V2 = count of leading letter of a word
 */
void Map(const string &name, const string &content)
{
    (void) name;
    const int n = content.size();
    for (int i = 0; i < n; ) {
        while ((i < n) && isspace(content[i])) {
            i++;
        }
        int start = i;
        while ((i < n) && !isspace(content[i])) {
            i++;
        }
        if (start < i) {
            count_map[map_current_split][content[start]]++;
        }
    }
}

/*
 * K2 = leading letter of a word
 * V2 = count of leading letter of a word
 * --> final result
 */
void Reduce(char c, int partial_count)
{
    result_map[c] += partial_count;
}

/*
 * See: http://en.wikipedia.org/wiki/MapReduce
 */
int main()
{
    const string contents[2] = {
        "aaa bbb ccc ddd eee fff ggg hhh",
        "this is a very very long text balah balah"
    };
    for (int i = 0; i < map_split_count; i++) {
        map_current_split = i;
        Map("", contents[i]);
    }
    for (int i = 0; i < map_split_count; i++) {
        for (int j = 0; j < reduce_split_count; j++) {
            Reduce('a'+j, count_map[i]['a'+j]);
        }
    }
    /* result */
    for (map<char, int>::const_iterator it = result_map.begin(); it != result_map.end(); it++) {
        if (it->second) {
            cout << it->first << "=" << it->second << endl;
        }
    }
    return 0;
}
